/**
 * Created by taohj on 2014/5/21.
 */

var dictionaryGroup = new Object();


/**
 * 模块列表
 * */
dictionaryGroup.initgrid = function () {
    dictionaryGroup.datagrid = $("#sys_dictionaryGroup_list_datagrid");
    dictionaryGroup.datagrid.datagrid({
        url: functions.get("SysDictionaryGroupList")+"",
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns:true,
        sortName:"id",
        sortOrder:"asc",
        fit: true,
        pageList: [20, 50, 100],
        pageSize: 20,

        onDblClickRow:function(rowIndex, rowData){
           dictionary.datagridDialog = $("<div></div>");
            dictionary.datagridDialog.dialog({
                title: 'SysDictionary &lt;'+rowData.name+'&gt; Management',
                href: functions.get('SysDictionaryManagemnet')+'?groupId='+rowData.id,
                modal: true,
                width: 600,
                height:400,
                top:100,
                onClose:function(){
                    dictionary.datagridDialog.dialog('destroy');
                }
            });
        },
        toolbar: [
            {
                iconCls: 'icon-add',
                text: 'Create',
                handler: function () {
                    var dialog = $("<div></div>");
                    dialog.dialog({
                        title: 'SysDictionaryCreate',
                        href:functions.get('SysDictionaryGroupCreateView'),
                        modal: true,
                        width: 300
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: 'Modify',
                handler: function () {
                    var selectRow= dictionaryGroup.datagrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                    var id="";
                    if(selectRow){
                        id=selectRow.id;
                    }

                    var dialog = $("<div></div>");
                    dialog.dialog({
                        title: 'SysDictionaryGroupModify',
                        href:functions.get('SysDictionaryGroupModifyView')+'?id='+id,
                        modal: true,
                        width: 300
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: 'Remove',
                handler: function () {

                    var selectRow= dictionaryGroup.datagrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                    $.messager.confirm('Confirm', 'Are you sure remove selected object?', function(r){
                        if (r){
                            $.post(functions.get('SysDictionaryGroupRemove'), {
                                id:selectRow.id
                            }, function (result) {
                                util.requestUtil(result,null,dictionaryGroup.datagrid);
                            });
                        }
                    });
                    console.info(selectRow);
                }
            }
        ],
        columns:[[
            {field:"id",title:"ID",width:60,sortable:true},
            {field:"name",title:"NAME",width:200,sortable:true},
            {field:"description",title:"DESCRIPTION"}
        ]]
    });
};


//新建提交
dictionaryGroup.createSubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get('SysDictionaryGroupCreate'), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,dictionaryGroup.datagrid)
        });
    }
};


//修改提交
dictionaryGroup.modifySubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get('SysDictionaryGroupModify'), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,dictionaryGroup.datagrid);
        });
    }
};


//搜索
dictionaryGroup.searchSubmit=function(button){
    var form =util.form(button);
    var params = util.serializeObject(form);
    dictionaryGroup.datagrid.datagrid("load",params);
}







